package com.duowei.diandan.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.duowei.diandan.R;
import com.duowei.diandan.bean.food.FoodCate;
import com.duowei.diandan.util.FoodHelper;

import java.util.List;

public class CateAdapter extends BaseAdapter {
    private static final String TAG = "CateAdapter";

    private Context mContext;
    private List<FoodCate> mList;
    private ListView mListView;

    public CateAdapter(Context context, List<FoodCate> list, ListView listView) {
        mContext = context;
        mList = list;
        mListView = listView;
    }

    public void updateData(List<FoodCate> newList) {
        if (!mList.containsAll(newList) || !newList.containsAll(mList)) {
            mList = newList;
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public FoodCate getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.d(TAG, "getView: position = " + position);

        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.header_list_item,
                    parent, false);
        }

        FoodCate item = getItem(position);

        ImageView imageView = convertView.findViewById(R.id.imageView);
        FoodHelper.setCoverImage(item.getDmjyxmsslb().getLbbm(), imageView);

        TextView textView = convertView.findViewById(R.id.textView);
        textView.setText(getItem(position).getDmjyxmsslb().getLbmc());

        int checked = mListView.getCheckedItemPosition();
        if (checked == position) {
            convertView.animate()
                    .setDuration(300)
                    .scaleX(1.2f)
                    .scaleY(1.2f);

            textView.setTypeface(Typeface.DEFAULT_BOLD);
        } else {
            convertView.setScaleX(1.0f);
            convertView.setScaleY(1.0f);

            textView.setTypeface(Typeface.DEFAULT);
        }

        return convertView;
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }
}
